package map.android.baidu.carpool.preferences;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.ArrayList;

import com.baidu.platform.comapi.basestruct.Point;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.text.TextUtils;

import map.android.baidu.carpool.utils.SafeUtils;
import map.android.baidu.carpool.widget.TextinputHistroyAndSugListView.TextInputSug;

public class SugHistoryPreferences {

	private final static String CAR_START_NODE_HISTORY = "start_node";
	private final static String CAR_END_NODE_HISTORY = "end_node";

	private final static String INDEX = "_index";

	private static String encodeToString(TextInputSug sug) {
		if (sug == null) {
			return "";
		}
		String s = "";
		try {
			s = URLEncoder.encode(sug.getName(), "utf-8") + "&" + URLEncoder.encode(sug.getAddress(), "utf-8") + "&"
					+ sug.getPoint().getDoubleX() + "&" + sug.getPoint().getDoubleY();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return s;
	}

	private static TextInputSug decodeToSug(String s) {
		if (TextUtils.isEmpty(s)) {
			return null;
		}
		TextInputSug sug = null;
		if (s.matches("[^&]+&{1}[^&]+&{1}[^&]+&{1}[^&]+")) {
			String array[] = s.split("&");
			if (array != null && array.length == 4) {
				try {
					sug = new TextInputSug();
					sug.setName(URLDecoder.decode(array[0], "utf-8"));
					sug.setAddress(URLDecoder.decode(array[1], "utf-8"));
					double x = SafeUtils.safeString2double(array[2]);
					double y = SafeUtils.safeString2double(array[3]);
					Point p = new Point((int)x, (int)y);
					sug.setPoint(p);
				} catch (UnsupportedEncodingException e) {
					e.printStackTrace();
				}
			}
		}
		return sug;
	}

	private static void writeSugHistory(Activity activity, String preferencesName, ArrayList<String> list) {
		SharedPreferences sharedPreferences = activity.getSharedPreferences(preferencesName, Context.MODE_PRIVATE);
		Editor editor = sharedPreferences.edit();
		int index = 0;
		int size = list.size() < 5 ? list.size() : 5;
		for (index = 0; index < size; index++) {
			editor.putString(preferencesName + (size - index), list.get(index));
		}
		editor.putInt(preferencesName + INDEX, index + 1);
		editor.commit();
	}

	public static void saveFromWhereSugHistory(Activity activity, TextInputSug sug) {
		if (activity == null || sug == null) {
			return;
		}
		String content = encodeToString(sug);
		ArrayList<String> historyList = getFromWhereList(activity);
		removeSameNodeAndAdd(content, historyList);
		writeSugHistory(activity, CAR_START_NODE_HISTORY, historyList);
	}

	public static void saveGoWhereSugHistory(Activity activity, TextInputSug sug) {
		if (activity == null) {
			return;
		}
		String content = encodeToString(sug);
		ArrayList<String> historyList = getGoWhereList(activity);
		for (int i = 0; i < historyList.size(); i++) {
		}
		removeSameNodeAndAdd(content, historyList);
		writeSugHistory(activity, CAR_END_NODE_HISTORY, historyList);
	}

	public static ArrayList<TextInputSug> getFromWhereHistoryList(Activity activity) {

		SharedPreferences sharedPreferences = activity.getSharedPreferences(CAR_START_NODE_HISTORY,
				Context.MODE_PRIVATE);
		int num = sharedPreferences.getInt(CAR_START_NODE_HISTORY + INDEX, 0);
		if (num == 0) {
			return null;
		} else {
			ArrayList<TextInputSug> startNodeList = new ArrayList<TextInputSug>();
			for (int i = 1; i <= num; i++) {
				String node = sharedPreferences.getString(CAR_START_NODE_HISTORY + i, null);
				if (node != null) {
					TextInputSug sug = decodeToSug(node);
					if (sug != null) {
						startNodeList.add(0, sug);
					}
				}
			}
			return startNodeList;
		}
	}

	public static ArrayList<TextInputSug> getGoWhereHistoryList(Activity activity) {
		SharedPreferences sharedPreferences = activity.getSharedPreferences(CAR_END_NODE_HISTORY, Context.MODE_PRIVATE);
		int num = sharedPreferences.getInt(CAR_END_NODE_HISTORY + INDEX, 0);
		if (num == 0) {
			return null;
		} else {
			ArrayList<TextInputSug> nodeList = new ArrayList<TextInputSug>();
			for (int i = 1; i <= num; i++) {
				String node = sharedPreferences.getString(CAR_END_NODE_HISTORY + i, null);
				if (node != null) {
					TextInputSug sug = decodeToSug(node);
					if (sug != null) {
						nodeList.add(0, sug);
					}
				}
			}
			return nodeList;
		}
	}

	private static ArrayList<String> getFromWhereList(Activity activity) {

		SharedPreferences sharedPreferences = activity.getSharedPreferences(CAR_START_NODE_HISTORY,
				Context.MODE_PRIVATE);
		int num = sharedPreferences.getInt(CAR_START_NODE_HISTORY + INDEX, 0);
		if (num == 0) {
			return new ArrayList<String>();
		} else {
			ArrayList<String> startNodeList = new ArrayList<String>();
			for (int i = 1; i <= num; i++) {
				String start_node = sharedPreferences.getString(CAR_START_NODE_HISTORY + i, null);
				if (start_node != null) {
					startNodeList.add(0, start_node);
				}
			}
			return startNodeList;
		}
	}

	private static ArrayList<String> getGoWhereList(Activity activity) {
		SharedPreferences sharedPreferences = activity.getSharedPreferences(CAR_END_NODE_HISTORY, Context.MODE_PRIVATE);
		int num = sharedPreferences.getInt(CAR_END_NODE_HISTORY + INDEX, 0);
		if (num == 0) {
			return new ArrayList<String>();
		} else {
			ArrayList<String> endNodeList = new ArrayList<String>();
			for (int i = 1; i <= num; i++) {
				String end_node = sharedPreferences.getString(CAR_END_NODE_HISTORY + i, null);
				if (end_node != null) {
					endNodeList.add(0, end_node);
				}
			}
			return endNodeList;
		}
	}

	private static void removeSameNodeAndAdd(String node, ArrayList<String> nodeList) {
		if (nodeList == null) {
			nodeList = new ArrayList<String>();
		} else {
			for (int index = 0; index < nodeList.size(); index++) {
				if (node.equals(nodeList.get(index))) {
					nodeList.remove(index);
				}
			}
			nodeList.add(0, node);
		}
	}

	public static void clearPreferenceFromWhereHistory(Activity activity) {
		SharedPreferences sharedPreferences = activity.getSharedPreferences(CAR_START_NODE_HISTORY,
				Context.MODE_PRIVATE);
		Editor editor = sharedPreferences.edit();
		editor.clear();
		editor.commit();
	}

	public static void clearPreferenceGoWhereHistory(Activity activity) {
		SharedPreferences sharedPreferences = activity.getSharedPreferences(CAR_END_NODE_HISTORY, Context.MODE_PRIVATE);
		Editor editor = sharedPreferences.edit();
		editor.clear();
		editor.commit();
	}
}
